from collections import Counter
from pyecharts.charts import Bar, Page
from pyecharts import options as opts


def number_analyse():
    red_balls = []
    blue_balls = []
    with open('history.txt', 'r') as f:
        for i in range(1860):
            oneLine_data = f.readline().strip()
            red_balls.extend(
                [int(oneLine_data[15 + (2 * i):15 + (2 * (i + 1))]) for i in range(5)])
            blue_balls.append(int(oneLine_data[-4:-2]))
            blue_balls.append(int(oneLine_data[-2:]))

    red_counter = Counter(red_balls)
    blue_counter = Counter(blue_balls)
    print(red_balls)
    print(blue_balls)
    print(red_counter)
    print(blue_counter)
    print(red_counter.most_common())
    print(blue_counter.most_common())
    red_dict = {}
    blue_dict = {}
    for i in red_counter.most_common():  # 使用collections模块的counter统计红球和蓝球各个号码的出现次数
        red_dict['{}'.format(i[0])] = i[1]
    for j in blue_counter.most_common():
        blue_dict['{}'.format(j[0])] = j[1]

    print(red_dict)
    print(blue_dict)

    red_list = sorted(red_counter.most_common(),
                      key=lambda number: number[0])  # 对红蓝球号码和次数重新进行排序
    blue_list = sorted(blue_counter.most_common(),
                       key=lambda number: number[0])
    print(red_list)
    print(blue_list)

    red_bar = Bar()
    red_x = ['{}'.format(str(x[0])) for x in red_list]
    red_y = ['{}'.format(str(x[1])) for x in red_list]
    red_bar.add_xaxis(red_x)
    red_bar.add_yaxis('红色球出现次数', red_y)
    red_bar.set_global_opts(title_opts=opts.TitleOpts(title='大乐透彩票', subtitle='近12年数据'), toolbox_opts=opts.ToolboxOpts(), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}/次")),
                            xaxis_opts=opts.AxisOpts(name='开奖号码'))
    red_bar.set_series_opts(markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_='max', name='最大值'),
              opts.MarkPointItem(type_='min', name='最小值')]
    ))
    blue_bar = Bar()
    blue_x = ['{}'.format(str(x[0])) for x in blue_list]
    blue_y = ['{}'.format(str(x[1])) for x in blue_list]
    blue_bar.add_xaxis(blue_x)
    blue_bar.add_yaxis('Blue出现次数', blue_y,
                       itemstyle_opts=opts.ItemStyleOpts(color='blue'))
    blue_bar.set_global_opts(title_opts=opts.TitleOpts(title='DLT彩票', subtitle='近12年数据'), toolbox_opts=opts.ToolboxOpts(), yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value}/次")),
                             xaxis_opts=opts.AxisOpts(name='开奖号码'))
    blue_bar.set_series_opts(markpoint_opts=opts.MarkPointOpts(
        data=[opts.MarkPointItem(type_='max', name='最大值'),
              opts.MarkPointItem(type_='min', name='最小值')]
    ))

    page = Page(page_title='DLT历史开奖数据分析', interval=3)
    page.add(red_bar, blue_bar)
    page.render('DLT历史开奖数据分析.html')


number_analyse()
